<?php

namespace App\Http\Controllers\Admin\Categories\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Categories;
use App\Model\Articles;
use Illuminate\Support\Facades\Redis;

class CategoriesApi extends Controller
{   
    /**
     * 获取分类列表信息
     * @return [array] 分类列表信息
     */
    public static function getCategoriesList( $type = 'all' )
    {      
        //如果获取的是全部分类
        if ($type == 'all') {

            //尝试从Redis获取分类信息
            if (Redis::get('categories-list-all')) {
                return json_decode(Redis::get('categories-list-all'), true);
            }

            //从数据库获取
            $categoriesList = Categories::select(['id', 'name', 'description', 'sort', 'status', 'updated_at'])
                    ->orderBy('sort', 'desc')
                    ->get()->toArray();

            //存入Redis
            Redis::set('categories-list-all', json_encode($categoriesList));


            return $categoriesList;

        } elseif ($type = 'active') {   //如果获取的是生效(status字段为1)的分类

            //尝试从Redis获取分类信息
            if (Redis::get('categories-list-active')) {
                return json_decode(Redis::get('categories-list-active'), true);
            }

            //从数据库获取
            $categoriesList = Categories::select(['id', 'name', 'description', 'sort', 'status', 'updated_at'])
                    ->where('status', 1)->orderBy('sort', 'desc')->get()->toArray();

            //存入Redis
            Redis::set('categories-list-active', json_encode($categoriesList));

            return $categoriesList;
        }
        
    }

    /**
     * 把用户输入的分类信息录入数据库
     * @param  Request $request 用户输入的分类信息
     * @return [array]           执行录入后的信息
     */
    public static function categoryStore(Request $request)
    {   
        //获取用户输入的分类信息
        $name = $request->input('name');
        $description = ($request->input('description') == null) ? '' : $request->input('description');
        $sort = ($request->input('sort') == null) ? 1 : $request->input('sort') ;
        $status = $request->input('status');

        //利用分类的模型执行录入操作
        $category = new Categories;
        $category->name = $name;
        $category->description = $description;
        $category->sort = $sort;
        $category->status = $status;

        //执行录入
        $bool = $category->save();

        if ($bool) {

            return [
                'code' => 200,
                'msg' => '新增分类成功',
            ];

        } else {

            return [
                'code' => 400,
                'msg' => '新增分类失败',
            ];

        }
    }

    /**
     * 获取单个分类的信息
     * @param  [num] $id 分类ID
     * @return [array]     某个分类的所有信息
     */
    public static function getCategoriesInfo($id)
    {
        $data = Categories::find($id)->toArray();

        return $data;
    }

    public static function categoryUpdate(Request $request)
    {
        //获取用户输入的分类信息
        $id = $request->input('id');
        $name = $request->input('name');
        $description = ($request->input('description') == null) ? '' : $request->input('description');
        $sort = ($request->input('sort') == null) ? 1 : $request->input('sort') ;
        $status = $request->input('status');

        //利用分类的模型执行录入操作
        $category = Categories::find($id);
        $category->name = $name;
        $category->description = $description;
        $category->sort = $sort;
        $category->status = $status;

        //执行录入
        $bool = $category->save();

        if ($bool) {

            return [
                'code' => 200,
                'msg' => '修改分类成功',
            ];

        } else {

            return [
                'code' => 400,
                'msg' => '修改分类失败',
            ];

        }
    }

    /**
     * 执行删除某个分类
     * @param  [num] $id 分类ID
     * @return [json] 执行后的信息
     */
    public static function categoryDel($id)
    {
        //先判断分类ID是否在文章表中存在
        $article = Articles::where(['category_id' => $id])->limit(1)->first();
        if ($article) {
            $result = [
                'code' => 300,
                'msg' => '该分类ID已经属于某个文章,无法删除',
            ];
            return response()->json($result);
        }

        //删除对应分类
        $bool = Categories::destroy($id);
        // $bool = true;

        if ($bool) {

            $result = [
                    'code' => 200,
                    'msg' => '删除分类ID成功',
                ];

            return response()->json($result);
        } else {

            $result = [
                    'code' => 400,
                    'msg' => '删除分类ID失败',
                ];

            return response()->json($result);
        }

    }



}
